package com.vids.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vids.mapper.ImageComparedDetailMapper;
import com.vids.model.entity.Ape;
import com.vids.model.entity.ImageComparedDetail;
import com.vids.model.entity.ImageComparedSummary;
import com.vids.model.entity.VIIDServer;
import com.vids.model.param.VIIDServerParam;
import com.vids.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhongbang
 * @since 2024-05-31
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/imageComparedDetail")
public class ImageComparedDetailController {

    @Resource
    ImageComparedDetailMapper detailMapper ;


    @ApiOperation(value = "查询比较结果详情", httpMethod = "get", notes = "查询比较结果详情")
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String s_apeIpAddr,
                              @RequestParam(defaultValue = "") String s_imageType,
                              @RequestParam(defaultValue = "") String s_comparedHour,
                              @RequestParam(defaultValue = "") String s_comparedResult,
                              @RequestParam(defaultValue = "") String startTime,
                              @RequestParam(defaultValue = "") String endTime) {


        LambdaQueryWrapper<ImageComparedDetail> wrapper = Wrappers.<ImageComparedDetail>lambdaQuery();

        wrapper.orderByDesc(ImageComparedDetail::getId);

        if (StrUtil.isNotBlank(search)) {
            wrapper.like(ImageComparedDetail::getApeId, search);
            log.info("search="+ search);
        }

        if (StrUtil.isNotBlank(s_apeIpAddr)) {
            wrapper.eq(ImageComparedDetail::getApeIpAddr, s_apeIpAddr);
            log.info("s_apeIpAddr="+ s_apeIpAddr);
        }

        if (StrUtil.isNotBlank(s_imageType) && (!s_imageType.contains("99"))) {
            wrapper.eq(ImageComparedDetail::getImageType, Integer.parseInt(s_imageType));
            log.info("s_imageType="+ s_imageType);
        }

        if (StrUtil.isNotBlank(s_comparedHour) && (!s_comparedHour.contains("24"))) {
            wrapper.eq(ImageComparedDetail::getComparedHour, Integer.parseInt(s_comparedHour));
            log.info("s_comparedHour="+ s_comparedHour);
        }

        if (StrUtil.isNotBlank(s_comparedResult)) {
            wrapper.eq(ImageComparedDetail::getComparedResult, Integer.parseInt(s_comparedResult));
            log.info("s_comparedResult="+ s_comparedResult);
        }

        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            wrapper.between(ImageComparedDetail::getApeImageCreateTime, startTime, endTime);
            log.info("startTime="+ startTime);
            log.info("endTime="+ endTime);
        }else if (StrUtil.isNotBlank(startTime))
        {
            wrapper.ge(ImageComparedDetail::getApeImageCreateTime,startTime);
        }else if (StrUtil.isNotBlank(endTime))
        {
            wrapper.le(ImageComparedDetail::getApeImageCreateTime,endTime);
        }

        Page<ImageComparedDetail> detailPage = detailMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        String image_full_name="";
        String image_full_name2="";
        String imageTypeStr_zh;
        String imageTypeStr_en;

        String  viid_root = "viid_picture/";
        String  ape_root = "picture/";

        for(ImageComparedDetail detail: detailPage.getRecords())
        {
            imageTypeStr_zh="";
            imageTypeStr_en="";
            image_full_name ="";
            image_full_name2 ="";

            //  目标类型 TargetType 1：人员，2：人脸，3：机动车，4：非机动车，5：物品，6：场景
            if (detail.getImageType() == 1)
            {
                imageTypeStr_zh= "人体";
                imageTypeStr_en="person";
            }
            else if (detail.getImageType() == 2)
            {
                imageTypeStr_zh= "人脸";
                imageTypeStr_en="face";
            } else if (detail.getImageType() == 3)
            {
                imageTypeStr_zh= "机动车";
                imageTypeStr_en="motor";
            }else if (detail.getImageType() == 4)
            {
                imageTypeStr_zh= "非机动车";
                imageTypeStr_en="non_motor";
            }else {
                imageTypeStr_zh= "未知类型";
                imageTypeStr_en="unknown";
            }

            detail.setImageTypeStr(imageTypeStr_zh);

            LocalDateTime ldt = detail.getApeImageCreateTime();
            if (ldt != null) {
                String date_path = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                image_full_name = ape_root + date_path + "/" + detail.getComparedHour() + "/" + imageTypeStr_en + "/" + detail.getApeId() + "/" + detail.getApeImage();
            }
            detail.setImageUrl(image_full_name);

            LocalDateTime ldt2 = detail.getViidImageCreateTime();
            if (ldt2 != null) {
                String date_path2 = ldt2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                image_full_name2 = viid_root + date_path2 + "/" + detail.getComparedHour() + "/" + imageTypeStr_en + "/" + detail.getApeId() + "/" + detail.getViidImage();
            }
            detail.setViidImageUrl(image_full_name2);

            if (detail.getComparedResult() == 0)
                detail.setComparedResultStr("相同");
            else
                detail.setComparedResultStr("不同");

        }
        log.info("detailPage  total="+ detailPage.getTotal());
        log.info("detailPage  pages="+ detailPage.getPages());
        return Result.success(detailPage);
    }
}
